package com.atghost.minio.controller;

import com.atghost.minio.dto.req.CompleteUploadChunkReq;
import com.atghost.minio.dto.req.StartChunkUploadReq;
import com.atghost.minio.dto.req.UploadChunkReq;
import com.atghost.minio.dto.req.VideoInfoReqDTO;
import com.atghost.minio.dto.res.FileInodeUploadRes;
import com.atghost.minio.dto.res.StartChunkUploadRes;
import com.atghost.minio.dto.res.UploadChunkRes;
import com.atghost.minio.service.MinioFileService;
import com.atghost.minio.util.response.RespResult;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Admin
 * @version 1.0
 * @date 2024-5-12 21:24
 **/
@RestController
@RequestMapping("/minio")
public class MinioFileController {

    @Resource
    private MinioFileService minioFileService;
    /***********************跳转接口*******************************/
    /**
     * 上传文件的入口:跳转上传接口
     **/
    @RequestMapping(value = "/home")
    public ModelAndView homeUpload() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("upload");
        return modelAndView;
    }
    @RequestMapping(value = "/video")
    public ModelAndView homeVideo() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("video");
        modelAndView.addObject("bucketName", "condor");
        modelAndView.addObject("objectName", "sha-256:4a:fa:bf:4afabf23c1e67f0e95baaccedba68390fb4abac1290970fc587f95ccaf608bc2.Lambda表达式使用.mp4");
        return modelAndView;
    }


    /*******************************业务接口***************************/
    /**
     * 开始分片上传：监测是否传过，以及未传的情况下的分片信息
     **/
    @PostMapping(value = "/startChunkUpload", consumes = "application/json")
    public RespResult<StartChunkUploadRes> startChunkUpload(@RequestBody StartChunkUploadReq startChunkUploadReq) {
        System.out.println(1);
        return RespResult.ok(minioFileService.startChunkUpload(startChunkUploadReq));
    }

    /**
     * 分片上传
     *
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping("/chunkUpload")
    public RespResult<UploadChunkRes> chunkUpload(HttpServletRequest request) throws IOException {

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        Integer chunkId = Integer.valueOf(multipartRequest.getParameter("x-chunkUpload-chunkId"));
        Long size = Long.valueOf(multipartRequest.getParameter("x-chunkUpload-size"));
        String id = multipartRequest.getParameter("x-chunkUpload-id");
        MultipartFile file = multipartRequest.getFile("file");

        InputStream inputStream = file.getInputStream();

        UploadChunkReq req = new UploadChunkReq();
        req.setFile(inputStream);
        req.setSize(size);
        req.setId(id);
        req.setChunkId(chunkId);

        UploadChunkRes res = minioFileService.chunkUpload(req);
        //关闭流
        inputStream.close();
        return RespResult.ok(res);
    }

    @ApiOperation("完成分片上传")
    @PostMapping("/completeChunkUpload")
    public RespResult<FileInodeUploadRes> completeChunkUpload(@RequestBody @Validated CompleteUploadChunkReq req) {
        FileInodeUploadRes uploadRes = minioFileService.completeChunkUpload(req);
        return RespResult.ok(uploadRes);
    }




    @GetMapping("/videoPlay/{bucketName}/{objectName}")
    public void videoPlay(HttpServletRequest req, HttpServletResponse res,
                          @PathVariable("bucketName") String bucketName,
                          @PathVariable("objectName") String objectName) {
        minioFileService.videoPlay(
                req,
                res,
                bucketName,
                objectName);
    }

}
